package com.mysql.mybatis.controller;

import com.mysql.mybatis.dao.model.User;
import com.mysql.mybatis.service.UserControllerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author : lhb
 * @date : 2019/11/5 10:55
 * @description: 访问地址：http://localhost:18888/swagger-ui.html
 */
@RestController
@RequestMapping(value = "/user")
@Slf4j
@Api(description = "用户管理")
public class UserController {

    @Autowired
    UserControllerService userControllerService;

    @GetMapping(value = "/get_all")
    @ApiOperation(value = "查询用户列表", response = ResponseEntity.class)
    public ResponseEntity getAllUser() {
        return ResponseEntity.status(HttpStatus.OK).body(userControllerService.getAllUsers());
    }

    @PostMapping(value = "/getByRowBounds")
    @ApiOperation(value = "通过Rowbounds分页查询用户列表", response = ResponseEntity.class)
    public ResponseEntity getByRowBounds(@RequestParam(value = "pageNum") Integer pageNum,
                                         @RequestParam(value = "pageSize") Integer pageSize) {
        return ResponseEntity.status(HttpStatus.OK).body(userControllerService.getByRowBounds(pageNum, pageSize));
    }

    @PostMapping(value = "/getByPageHelper")
    @ApiOperation(value = "通过Rowbounds分页查询用户列表", response = ResponseEntity.class)
    public ResponseEntity getByPageHelper(@RequestParam(value = "pageNum") Integer pageNum,
                                          @RequestParam(value = "pageSize") Integer pageSize) {
        return ResponseEntity.status(HttpStatus.OK).body(userControllerService.getByPageHelper(pageNum, pageSize));
    }

    @PostMapping(value = "/save")
    @ApiOperation(value = "插入", response = ResponseEntity.class)
    public ResponseEntity save(@RequestBody User user) {
        userControllerService.save(user);
        return ResponseEntity.status(HttpStatus.OK).body("success");
    }

    @PostMapping(value = "/getById")
    @ApiOperation(value = "查询", response = ResponseEntity.class)
    public ResponseEntity getById(@RequestParam(value = "id") Long id) {
        return ResponseEntity.status(HttpStatus.OK).body(userControllerService.getById(id));
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "修改用户", response = ResponseEntity.class)
    public ResponseEntity update(@RequestBody User user) {
        userControllerService.update(user);
        return ResponseEntity.status(HttpStatus.OK).body("success");
    }

    @PostMapping(value = "/deleteById")
    @ApiOperation(value = "删除", response = ResponseEntity.class)
    public ResponseEntity deleteById(@RequestParam(value = "id") Long id) {
        userControllerService.deleteById(id);
        return ResponseEntity.status(HttpStatus.OK).body("success");
    }
}
